//
//  CenterTextCellData.m
//  18Birdies
//
//  Created by weihua on 2017/5/7.
//  Copyright © 2017年 Lc. All rights reserved.
//

#import "CenterTextCellData.h"
#import "CenterTextCell.h"
#import "LcFoundation.h"

@implementation CenterTextCellData

- (Class)cellClazz {
  return CenterTextCell.self;
}

- (NSString*)nibName {
  return @"CenterTextCell";
}

- (instancetype)initWithTitle:(NSString*)title line:(int)line {
  self = [super init];
  if (self) {
    self.title = title;
//    self.cellHeight = 44;
    self.lines = line;
    self.titleEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 0);
  }
  return self;
}

- (instancetype)initWithAttributeTitle:(NSAttributedString*)attributeTitle line:(int)line {
  self = [super init];
  if (self) {
    self.attributeTitle = attributeTitle;
    self.lines = line;
    self.titleEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 0);
    self.cellHeight = [attributeTitle boundingRectWithSize:CGSizeMake([LcUIUtility screenWidth] - 60, 0) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom;
  }
  return self;
}

- (void)refreshCell:(CenterTextCell *)cell {
  [super refreshCell:cell];
 
  if (self.bgColor != nil) {
    [cell setBackgroundColor:self.bgColor];
  }
  
  if (self.title) {
    [cell.textLabel setAttributedText:nil];
    [cell.titleLabel setText:self.title];
    if (self.titleFont != nil) {
      [cell.titleLabel setLcFont:self.titleFont];
    }
  } else if (self.attributeTitle) {
    [cell.titleLabel setText:nil];
    [cell.textLabel setAttributedText:self.attributeTitle];
  }
  
  cell.textLabel.numberOfLines = self.lines;
  cell.textLabel.textAlignment = NSTextAlignmentCenter;
  cell.titleLabelTopConstraint.constant = self.titleEdgeInsets.top;
  cell.titleLabelBottomConstraint.constant = self.titleEdgeInsets.bottom;
  
  if (_isItalic) {
    CGFloat size = 17;
    if ([UIDevice is5UISize]) {
      size = 14;
    }
    [cell.titleLabel setFont:[self getItasicFont:size]];
  }
}

- (UIFont*)getItasicFont:(CGFloat)size {
  CGAffineTransform matrix = CGAffineTransformMake(1, 0, (CGFloat)tanf(10 * M_PI / 180), 1, 0, 0);
  UIFontDescriptor* desc = [UIFontDescriptor fontDescriptorWithName:@"" matrix:matrix];
  UIFont *font = [UIFont fontWithDescriptor:desc size:size];
  return font;
}

@end
